package com.xunlai.infra.permission.manager.web.service.impl;

import com.xunlai.infra.permission.common.exception.AccountAlreadyExistsException;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.converter.UserAccountConverter;
import com.xunlai.infra.permission.manager.web.service.AdminUserService;
import com.xunlai.infra.permission.manager.web.vo.BasePageRespVo;
import com.xunlai.infra.permission.manager.web.vo.user.CreateUserAccountReqVo;
import com.xunlai.infra.permission.manager.web.vo.user.UserAccountVo;
import com.xunlai.infra.permission.storage.entity.UserAccount;
import com.xunlai.infra.permission.storage.repository.UserAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liang
 * @date 12/8/22 10:22 PM
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdminUserServiceImpl implements AdminUserService {

    private UserAccountRepository userAccountRepository;

    private PasswordEncoder passwordEncoder;

    @Override
    public Long createUserAccount(CreateUserAccountReqVo createUserAccountReqVo) throws PermissionBusinessException {

        UserAccount convert = UserAccountConverter.INSTANCE.convertCreateReq2Account(createUserAccountReqVo);
        boolean exists = userAccountRepository.existsByAccount(convert.getAccount());
        if(exists){
            throw new AccountAlreadyExistsException();
        }
        convert.setPassword(passwordEncoder.encode(convert.getPassword()));
        convert.setStatus(UserAccount.UserStatus.NORMAL.getStatus());
        convert.setDeleted((short)0);
        UserAccount save = userAccountRepository.save(convert);
        return save.getId();
    }

    @Override
    public BasePageRespVo<UserAccountVo> pageListUserAccount(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int size = (pageSize == null || pageSize <= 0) ? 10 : pageSize;
        Page<UserAccount> userAccountPage = userAccountRepository.findAll(PageRequest.of(pageNum-1, size));
        List<UserAccount> userAccountList = userAccountPage.getContent();
        List<UserAccountVo> userAccountVoList = userAccountList.stream().map(UserAccountConverter.INSTANCE::convertAccount2ResponseVo).toList();
        return new BasePageRespVo<>(userAccountVoList,pageNum,size,userAccountPage.getTotalElements(), userAccountPage.getTotalPages());
    }

    @Autowired
    public void setUserAccountRepository(UserAccountRepository userAccountRepository) {
        this.userAccountRepository = userAccountRepository;
    }

    @Autowired
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
}
